home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / evntsdev / focevent.sx < prev    next >
Encoding:
Text File  |  1996-05-21  |  3.5 KB  |  123 lines  |  [TEXT/ttxt]

  1. -- <<<-
  2.  
  3. -- focevent.sx
  4. -- Focus Event example
  5. -- by Howard Metzenberg
  6. -- Kaleida ScriptX forever
  7. -- from "Events and Input Devices" chapter of
  8. -- ScriptX Components Guide
  9.  
  10. -- This script demonstrates how to manage keyboard focus
  11. -- on a presenter other than TextEdit
  12.  
  13. -- Note that this script has changed since ScriptX 1.0
  14. -- Now you must force focus on a presenter other than TextEdit
  15.  
  16.  
  17. module EventTest3 uses ScriptX end
  18. in module EventTest3
  19.  
  20. class FocusedPresenter (TwoDShape)
  21.     class variables
  22.         focusManager:((new KeyboardDevice).focusManager)
  23.     instance variables
  24.         mouseInterest
  25.         focusInterest
  26.     instance methods
  27.         method init self #rest args -> (
  28.             apply nextMethod self args
  29.             self.focusInterest := new FocusEvent
  30.             self.focusInterest.authorData := self
  31.             self.focusInterest.device := new KeyboardDevice
  32.             self.focusInterest.presenter := self
  33.             self.focusInterest.eventReceiver := processFocus
  34.             addEventInterest self.focusInterest
  35.             self.mouseInterest := new MouseDownEvent
  36.             self.mouseInterest.authorData := self
  37.             self.mouseInterest.presenter := self
  38.             self.mouseInterest.eventReceiver := processMouse
  39.             addEventInterest self.mouseInterest
  40.         )
  41.         -- event receiver for focus events
  42.         method processFocus self interest event -> (
  43.             if (event.focusType = @loseFocus) then (
  44.                 -- we lost focus, so show that it is disabled
  45.                 event.presenter.fill := whiteBrush
  46.             ) else ( 
  47.                 -- we gained focus, so be colorful!
  48.                 local fillcolor := new RGBColor \
  49.                         red:(rand 255) green:(rand 255) blue:(rand 255)
  50.                 event.presenter.fill := new Brush color:fillcolor 
  51.                 @accept -- accept the event
  52.             )
  53.         )
  54.         -- event receiver for mouse events
  55.         method processMouse self interest event -> (
  56.             forceFocus FocusedPresenter.focusManager self
  57.             @accept -- accept the event
  58.         )
  59. end
  60.  
  61.  
  62. object firstRect (FocusedPresenter)
  63.     boundary:(new rect x2:200 y2:50), fill:whitebrush, stroke:blackbrush
  64.     settings x:50, y:50
  65. end
  66. object secondRect (FocusedPresenter)
  67.     boundary:(new rect x2:200 y2:50), fill:whitebrush, stroke:blackbrush
  68.     settings x:50, y:150
  69. end
  70. object thirdRect (FocusedPresenter)
  71.     boundary:(new rect x2:50 y2:250), fill:whitebrush, stroke:blackbrush
  72.     settings x:100, y:25
  73. end
  74.  
  75. -- now set up a window 
  76. object myWindow (Window)
  77.     boundary:(new Rect x2:400 y2:300), fill:whitebrush
  78.     settings x:16, y:40
  79. end
  80. append myWindow firstRect -- add it to the space
  81. append myWindow secondRect -- add it to the space
  82. append myWindow thirdRect -- add it to the space
  83. show myWindow
  84.  
  85.  
  86. -- example with TextEdit
  87. module EventTest4 uses ScriptX end
  88. in module EventTest4
  89.  
  90.  
  91. object myWindow (Window)
  92.     boundary:(new Rect x2:600 y2:300), fill:whitebrush
  93.     settings x:16, y:40
  94. end
  95. object myCursor (Line) x2:0, y2:16 end
  96. global testString1 := new Text string:"edit me, pretty please!"
  97. global testString2 := new Text string:"oh no, edit me!"
  98. global testString3 := new Text string:"please, edit me first!"
  99. object textBox1 (TextEdit)
  100.     boundary:(new Rect x2:384 y2:60)
  101.     target:testString1, stroke:blackBrush
  102.     settings x:128, y:64, cursor:myCursor
  103. end
  104. setDefaultAttr textBox1 @alignment @center
  105. object textBox2 (TextEdit)
  106.     boundary:(new Rect x2:384 y2:60)
  107.     target:testString2, stroke:blackBrush
  108.     settings x:128, y:128, cursor:myCursor
  109. end
  110. setDefaultAttr textBox2 @alignment @center
  111. object textBox3 (TextEdit)
  112.     boundary:(new Rect x2:384 y2:60)
  113.     target:testString3, stroke:blackBrush
  114.     settings x:128, y:192, cursor:myCursor
  115. end
  116. setDefaultAttr textBox3 @alignment @center
  117. append myWindow textBox1
  118. append myWindow textBox2
  119. append myWindow textBox3
  120. show myWindow
  121.  
  122. -- >>>
  123.